﻿using Grow.Module.Service;
using Grow.Module.Thread;
using Microsoft.AspNetCore.Http;

namespace Grow.AspNetCore.Thread
{
    public class CancellationTokenContext : ICancellationTokenContext
    {

        /// <summary>
        /// Http上下文访问器
        /// </summary>
        private readonly IHttpContextAccessor _httpContextAccessor;


        public CancellationTokenContext(IServiceContext serviceContext)
        {
            _httpContextAccessor = serviceContext.GetRequiredService<IHttpContextAccessor>();
        }

        /// <summary>
        /// 异步任务取消令牌
        /// </summary>
        public CancellationToken Token => _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None;
    }
}
